home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / tcp_ip / tnos / tnos100s / uuencode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-30  |  2.7 KB  |  116 lines

  1. /*
  2.  * Copyright (c) 1983 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Berkeley.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  */
  17.  
  18. #ifndef lint
  19. static char sccsid[] = "@(#)uuencode.c    5.6 (Berkeley) 7/6/88";
  20. #endif /* not lint */
  21.  
  22. /*
  23.  * uuencode [input] output
  24.  *
  25.  * Encode a file so it can be mailed to a remote system.
  26.  */
  27. #include <stdio.h>
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30.  
  31. #ifdef NOPE
  32. /* ENC is the basic 1 character encoding function to make a char printing */
  33. #define ENC(c) ((c) ? ((c) & 077) + ' ': '`')
  34. #else
  35. /* ENC is the basic 1 character encoding function to make a char printing */
  36. #define ENC(c) (((c) & 077) + ' ')
  37. #endif
  38.  
  39. main(argc, argv)
  40. char **argv;
  41. {
  42.     FILE *in;
  43.     struct stat sbuf;
  44.     int mode;
  45.  
  46.     /* optional 1st argument */
  47.     if (argc > 2) {
  48.         if ((in = fopen(argv[1], "rb")) == NULL) {
  49.             perror(argv[1]);
  50.             exit(1);
  51.         }
  52.         argv++; argc--;
  53.     } else
  54.         in = stdin;
  55.  
  56.     if (argc != 2) {
  57.         fprintf(stderr,"Usage: uuencode [infile] remotefile\n");
  58.         exit(2);
  59.     }
  60.  
  61.     /* figure out the input file mode */
  62.     if (fstat(fileno(in), &sbuf) < 0 || !isatty(fileno(in)))
  63.         mode = 0666 & ~umask(0666);
  64.     else
  65.         mode = sbuf.st_mode & 0777;
  66.     printf("begin %o %s\n", mode, argv[1]);
  67.  
  68.     encode(in, stdout);
  69.  
  70.     printf("end\n");
  71.     exit(0);
  72. }
  73.  
  74. /*
  75.  * copy from in to out, encoding as you go along.
  76.  */
  77. encode(in, out)
  78. register FILE *in;
  79. register FILE *out;
  80. {
  81.     char buf[80];
  82.     register int i, n;
  83.  
  84.     for (;;) {
  85.         /* 1 (up to) 45 character line */
  86.         n = fread(buf, 1, 45, in);
  87.         putc(ENC(n), out);
  88.  
  89.         for (i=0; i<n; i += 3)
  90.             outdec(&buf[i], out);
  91.  
  92.         putc('\n', out);
  93.         if (n <= 0)
  94.             break;
  95.     }
  96. }
  97.  
  98. /*
  99.  * output one group of 3 bytes, pointed at by p, on file f.
  100.  */
  101. outdec(p, f)
  102. register char *p;
  103. register FILE *f;
  104. {
  105.     register int c1, c2, c3, c4;
  106.  
  107.     c1 = *p >> 2;
  108.     c2 = (*p << 4) & 060 | (p[1] >> 4) & 017;
  109.     c3 = (p[1] << 2) & 074 | (p[2] >> 6) & 03;
  110.     c4 = p[2] & 077;
  111.     putc(ENC(c1), f);
  112.     putc(ENC(c2), f);
  113.     putc(ENC(c3), f);
  114.     putc(ENC(c4), f);
  115. }
  116.